package com.example.security;


import com.example.domain.po.*;
import com.example.mapper.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor // 自动注入
@Transactional // 开启事务
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserMapper userMapper;
    private final UserRoleMapper userRoleMapper;
    private final RoleMapper roleMapper;
    private final RolePermissionMapper rolePermissionMapper;
    private final PermissionMapper permissionMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.getUserByUsername(username);
        //判断当前账号是否存在
        if(Objects.isNull(user)){
            //如果为空，直接抛出异常
            throw new UsernameNotFoundException(username);
        }
        //角色名称集合
        List<String> roleNames=new ArrayList<>();
        //权限名称集合
        List<String> permissionNames=new ArrayList<>();

        //根据用户id获取用户角色关联列表
        List<UserRole> userRoles = userRoleMapper.getUserRolesByUserId(user.getId());
        if(!CollectionUtils.isEmpty(userRoles)){
            //获取角色id
            List<Integer> roleIds = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toList());
            //根据角色id获取角色信息
            List<Role> roles = roleMapper.batchGetRolesByRoleId(roleIds);
            if(!CollectionUtils.isEmpty(roles)){
                //获取角色(英文)名称
                List<String> roleList= roles.stream().map(Role::getRole).collect(Collectors.toList());
                //添加角色名称
                roleNames.addAll(roleList);
            }

            //根据角色id查询角色权限关联 集合
            List<RolePermission> rolePermissions = rolePermissionMapper.getRolePermissionsByRoleIds(roleIds);
            if(!CollectionUtils.isEmpty(rolePermissions)){
                //获取权限id
                List<Integer> permissionIds = rolePermissions.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
                //根据权限id获取权限信息
                List<Permission> permissions = permissionMapper.batchGetPermissionsByPermissionId(permissionIds);
                if(!CollectionUtils.isEmpty(permissions)){
                    //获取权限名称
                    List<String> permissionList= permissions.stream().map(Permission::getPermissionName).collect(Collectors.toList());
                    //添加权限名称
                    permissionNames.addAll(permissionList);
                }
            }
        }
        return new LoginUserDetails(user, roleNames, permissionNames);
    }
}
